package com.lapula.crazyface.util;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;

/**
 * class for pushing every new Activity instance to the stack and 
 * finishing them when user wants to quit the application.
 */
public class ActivityStack {
	private static List<Activity> activityList;
	private static ActivityStack instance;
	
	/**
	 * constructor
	 */
	private ActivityStack() {
		activityList = new ArrayList<Activity>();
	}
	
	/**
	 * get a singleton instance of ActivityStack
	 * @return: ActivityStack instance
	 */
	public static ActivityStack getInstance() {
		if (instance == null) {
			synchronized (ActivityStack.class) {
				instance = new ActivityStack();
			}
		}
		return instance;
	}
	
	/**
	 * push an activity to the stack
	 * @param activity
	 */
	public void add(Activity activity) {
		activityList.add(activity);
	}
	
	/**
	 * finish all the activities in the stack
	 */
	public void killAll() {
		int size = activityList.size();
		for (int i = 0; i < size; i++) {
			Activity activity = activityList.get(i);
			if (activity != null && !activity.isFinishing()) {
				activity.finish();
			}
		}
		System.exit(0);
	}
}
